package com.weight.system.project.web;

import com.weight.system.project.PO.FileInfo;
import com.weight.system.project.VO.FileInfoVO;
import com.weight.system.project.service.FileInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import org.springframework.ui.Model;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;
import com.weight.common.exception.CSException;

/**
 * 项目附件信息表
 */
@RestController
@Slf4j
@RequestMapping("/project/fileInfo")
public class FileInfoController {

	@Autowired
	private  FileInfoService fileInfoService;
	
	 private static FileInfoController fileInfoController;
	 @PostConstruct  
	 public void init() {      
		 
		 fileInfoController=this;
		 fileInfoController.fileInfoService=this.fileInfoService;
		 
	 }

	/**
	 * 1.查询项目附件信息表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForFileInfoList",method=RequestMethod.GET)
	public ModelAndView queryForFileInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/project/queryForFileInfoList");
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 2.查询项目附件信息表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForFileInfoListDG",method=RequestMethod.GET)
	public String queryForFileInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String fileName = request.getParameter("fileName");
String filePath = request.getParameter("filePath");
String fileDesc = request.getParameter("fileDesc");
String fileType = request.getParameter("fileType");
String fileSize = request.getParameter("fileSize");
String fileSort = request.getParameter("fileSort");
String typeCode = request.getParameter("typeCode");

			Map<Object, Object> map =new HashMap<>();
			map.put("fileName", fileName);
map.put("filePath", filePath);
map.put("fileDesc", fileDesc);
map.put("fileType", fileType);
map.put("fileSize", fileSize);
map.put("fileSort", fileSort);
map.put("typeCode", typeCode);

			
			//2、查询项目附件信息表列表
			List<FileInfo> fileInfoList = fileInfoService.findFileInfoByPage(page,rows,map);
			//3、返回前台
			PageResult<FileInfo> result = new PageResult<FileInfo>(fileInfoList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 3.查询项目附件信息表--详情页
	 * @param fileId
	 * @return
	 */
	@RequestMapping("/queryForFileInfoDetail")
	public ModelAndView queryForFileInfoDetail(@RequestParam(value="fileId",required=false) String fileId){
			//1.校验id
			CommonUtils.validateExistID(fileId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("fileId", fileId);
			log.info("查询项目附件信息表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			FileInfo fileInfo = fileInfoService.findByFileInfoId(map);
			
			//3、返回前台
			log.info("查询项目附件信息表查询成功：fileInfo={}",fileInfo);
			modelAndView.setViewName("/system/project/queryForFileInfoDetail");
			modelAndView.addObject("fileInfo",fileInfo);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改项目附件信息表操作
	 * @param fileId
	 * @return
	 */
	@RequestMapping(value="/addFileInfo")
	public ModelAndView addFileInfo(@RequestParam(value="fileId",required=false) String fileId){
			//1、创建返回对象
			FileInfoVO fileInfoVO = new FileInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(fileId)) {
				Map<String, String> map=new HashMap<>();
				map.put("fileId", fileId);
				log.info("修改操作，查询ID：fileId={}",fileId);
				FileInfo fileInfo = fileInfoService.findByFileInfoId(map);
				BeanUtils.copyProperties(fileInfo,fileInfoVO);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/project/addFileInfo");
			modelAndView.addObject("fileInfoVO",fileInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改项目附件信息表操作
	 * @param fileInfoVO
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value="/saveFileInfo")
	public JsonResultUtil saveFileInfo(@Valid FileInfoVO fileInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,fileInfoVO);
			
			//2.获取id
			String fileId=fileInfoVO.getFileId();
			FileInfo fileInfo=new FileInfo();
			BeanUtils.copyProperties(fileInfoVO,fileInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(fileId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("fileId", fileId);
				log.info("修改操作，查询ID：fileId={}",fileId);
				//查询是否存在
				fileInfoService.findByFileInfoId(map);
				try{
					fileInfo.setUpdateBy("000");
					fileInfoService.updateFileInfo(fileInfo);
					log.info("更新操作成功：fileInfo={}",fileInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					fileInfo.setFileId(CommonUtils.getUUId());
					fileInfo.setCreateBy("000");
					fileInfo.setUpdateBy("000");
					fileInfoService.insertFileInfo(fileInfo);
					log.info("添加操作成功：fileInfo={}",fileInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除项目附件信息表操作
	 * @param fileId
	 * @return
	 */
	@RequestMapping("/deleteByFileInfoId")
	public JsonResultUtil deleteByFileInfoId(@RequestParam(value="fileId",required=false) String fileId){
			//1.校验id
			CommonUtils.validateExistID(fileId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("fileId", fileId);
			fileInfoService.findByFileInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除项目附件信息表入参，map={}",map);
			
			try{
				fileInfoService.upDelByFileInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除项目附件信息表操作---批量删除
	 * @param fileIdList
	 * @return
	 */
	@RequestMapping("/deleteByFileInfoIdBatch")
	public JsonResultUtil deleteByFileInfoIdBatch(@RequestParam(value="fileIdList",required=false) List fileIdList){
			//1.校验id
			CommonUtils.validateExistIDList(fileIdList);
			
			//2.校验数据库
			fileInfoService.findByFileInfoIdList(fileIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("fileIdList", fileIdList);
			map.put("updateBy","000");
			log.info("批量删除项目附件信息表操作入参，map={}",map);
			
			try{
				fileInfoService.upDelByFileInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}
	@Transactional
	public static String saveFileInfoByUtil(FileInfo fileInfo){
		try{
			fileInfo.setFileId(CommonUtils.getUUId());
			fileInfo.setCreateBy("000");
			fileInfo.setUpdateBy("000");
//			fileInfoService.insertFileInfo(fileInfo);
			fileInfoController.fileInfoService.insertFileInfo(fileInfo);
			log.info("添加操作成功：fileInfo={}",fileInfo);
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage(), e);
			throw new CSException("文件保存失败！");
		}
		return "success";
	}
}